﻿#include "UserManagerForm.h"
#include"UserInfoForm.h"
#include<QStringListModel>
#include <QMessageBox>
#include<qDebug>
#include <QStandardItemModel>
#include <QMenu>
#include <QMessageBox>
#include <dialogusermdify.h>
#include <QFile>
#include <QTableWidgetItem>
#include"Tool/qfilehelper.h"
#pragma execution_character_set("utf-8")
UserManagerForm::UserManagerForm(QWidget* parent)
    : QDialog(parent)
{
    ui.setupUi(this);

    InitMenu();
    connect(ui.tableView, &QTableView::customContextMenuRequested,this, &UserManagerForm::slotShowMenu);

    load();
    UpdateUI();

    // 设置QTableView为单行选中模式
    ui.tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    // 设置QTableView为行选中模式
    ui. tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
}

UserManagerForm::~UserManagerForm()
{}

void UserManagerForm::setUserInfo(QList<UserInfo>& listUsers)
{
    m_listUserInfo = listUsers;
    UpdateUI();
}

QList<UserInfo>& UserManagerForm::getUserInfos()
{
    return m_listUserInfo;
}

void UserManagerForm::UpdateUI()
{
    // 创建模型
    QStandardItemModel* model = new QStandardItemModel();
    model->setColumnCount(4);

    // 添加表头
    QStringList tableHeader;
    tableHeader<<"头像"<<"用户名"<<"密码"<<"权限";
    model->setHorizontalHeaderLabels(tableHeader);

    // 设置行数
    int userCount =m_listUserInfo.count();
    model->setRowCount(userCount);
    for (int i = 0; i < userCount; ++i)
    {
        //1.头像
        QStandardItem *iconItem = new QStandardItem();
        iconItem->setIcon(QIcon(m_listUserInfo[i].iconPath()));
        iconItem->setSizeHint(QSize(50,50));
        model->setItem(i, 0, iconItem);
        //2.用户名
        model->setItem(i, 1, new QStandardItem(m_listUserInfo[i].name()));
            //3.密码
        model->setItem(i, 2, new QStandardItem("***"));
            //4.权限
        model->setItem(i, 3, new QStandardItem(m_listUserInfo[i].OperatorLevel()));
    }
    // 设置模型
    ui.tableView->setModel(model);

}

int UserManagerForm::GetSelectIndex()
{
    return 0;
}

QModelIndex UserManagerForm::getSelectItem()
{
    // 获取 QItemSelectionModel
    QItemSelectionModel* selectionModel = ui.tableView->selectionModel();
    // 获取选中的索引
    QModelIndexList selectedIndexes = selectionModel->selectedIndexes();
    if (!selectedIndexes.isEmpty())
    {

        return selectedIndexes.first();

    }
    return QModelIndex();
}

void UserManagerForm::ShowUserInfo(const UserInfo &userInfo)
{

}

void UserManagerForm::InitMenu()
{
    ui.tableView->setContextMenuPolicy(Qt::CustomContextMenu);
    m_menu = new QMenu(ui.tableView);
    m_pActionAdd = new QAction(tr("增加"));
    m_pActionDelete = new QAction(tr("删除"));
    m_pActionEdit = new QAction(tr("编辑"));
    m_pActionView = new QAction(tr("修改"));
    m_pActionSave = new QAction(tr("保存"));

    m_menu->addAction(m_pActionAdd);
    m_menu->addAction(m_pActionDelete);
    m_menu->addAction(m_pActionEdit);
    m_menu->addAction(m_pActionView);
    m_menu->addAction(m_pActionSave);

    connect(m_pActionAdd,SIGNAL(triggered(bool)),this,SLOT(slotMenuAdd()));
    connect(m_pActionDelete,SIGNAL(triggered(bool)),this,SLOT(slotMenuDelete()));
    connect(m_pActionEdit,SIGNAL(triggered(bool)),this,SLOT(slotMenuEdit()));
    connect(m_pActionView,SIGNAL(triggered(bool)),this,SLOT(slotMenuView()));
    connect(m_pActionSave,SIGNAL(triggered(bool)),this,SLOT(slotSave()));
}

void UserManagerForm::load()
{
    QFile readFile(QApplication::applicationDirPath() + "/UserInfo.dat");
    if (!readFile.exists())
    {
        UserInfo userinfo;
        userinfo.setName("admin");
        userinfo.setPassWord("admin");
        m_listUserInfo.append(userinfo);
        save();
        return;
    }
    else
    {
        //用户信息文件加载
        readFile.open(QIODevice::ReadOnly);
        QDataStream in(&readFile);
        UserInfo userinfo;
        while (true)
        {
            if (in.atEnd())
            {
                readFile.close();
                break;
            }
            in >> userinfo;
            m_listUserInfo.append(userinfo);

        }

        if(m_listUserInfo.count()==0)
        {
            UserInfo userinfo;
            userinfo.setName("admin");
            userinfo.setPassWord("admin");
            m_listUserInfo.append(userinfo);
            save();
        }
        if (readFile.isOpen())
        {
            readFile.close();
        }
    }
}

void UserManagerForm::save()
{
    QFile writeFile(QApplication::applicationDirPath() + "/UserInfo.dat");
    writeFile.open(QIODevice::WriteOnly);
    QDataStream out(&writeFile);
    foreach (UserInfo var ,m_listUserInfo)
    {
        out << var;
    }

    writeFile.close();
}

void UserManagerForm::slotShowMenu(const QPoint& pos)
{
    m_menu->exec(QCursor::pos());
}

void UserManagerForm::slotMenuAdd()
{
    DialogUserMdify modify;
    int result =modify.exec();
    if(result==QDialog::Accepted)
    {
        UserInfo info = modify.GetUserInfo();
        m_listUserInfo.append(info);
        UpdateUI();
    }
}

void UserManagerForm::slotMenuDelete()
{
    // 获取QTableView的选中模型
    QItemSelectionModel *selectionModel = ui.tableView->selectionModel();

    // 假设用户已经选择了一些行，以下是如何获取选中行的代码
    QModelIndexList selected = selectionModel->selectedRows();
    int rowId = selected.first().row();
    m_listUserInfo.removeAt(rowId);
    UpdateUI();
}

void UserManagerForm::slotMenuEdit()
{
    // 获取QTableView的选中模型
    QItemSelectionModel *selectionModel = ui.tableView->selectionModel();

    // 假设用户已经选择了一些行，以下是如何获取选中行的代码
    QModelIndexList selected = selectionModel->selectedRows();
    int rowId = selected.first().row();

    //编辑
    DialogUserMdify modify;
    modify.setUserInfo(m_listUserInfo[rowId]);
    int result =modify.exec();
    if(result==QDialog::Accepted)
    {
        UserInfo info = modify.GetUserInfo();
        m_listUserInfo[rowId]=info;
        UpdateUI();
    }
}

void UserManagerForm::slotMenuView()
{
    // 获取QTableView的选中模型
    QItemSelectionModel *selectionModel = ui.tableView->selectionModel();
    // 假设用户已经选择了一些行，以下是如何获取选中行的代码
    QModelIndexList selected = selectionModel->selectedRows();
    int rowId = selected.first().row();
    DialogUserMdify modify;
    modify.setUserInfo(m_listUserInfo[rowId]);
    int result =modify.exec();
}

void UserManagerForm::slotSave()
{
    save();
}
